package com.beaven.imclient.fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.CategoryActivity;
import com.beaven.imclient.adapter.UserSearchAdapter;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.presenter.user.FollowContract;
import com.beaven.imclient.presenter.user.FollowPresenter;
import com.beaven.imclient.utils.ToastUtil;
import com.beaven.imclient.widget.DividerDecoration;
import com.beaven.model.model.card.UserCard;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FollowFragment extends BaseFragment
    implements FollowContract.View, UserSearchAdapter.OnAddUserListener {

  public static FollowFragment newInstance() {
    return new FollowFragment();
  }

  public static final int CATEGORY_RESULT = 1;

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CATEGORY_RESULT && resultCode == Activity.RESULT_OK) {
      String categoryId = data.getStringExtra(CategoryActivity.CATEGORY_RESULT);
      presenter.addFriend(userCard.getId(), categoryId);
    } else {
      super.onActivityResult(requestCode, resultCode, data);
    }
  }

  private ProgressDialog dialog;
  private FollowContract.Presenter presenter;
  private EditText edit_search_user;
  private RecyclerView recycler_user;
  private TextView view_empty;
  private UserSearchAdapter adapter;
  private UserCard userCard;

  @Override protected int getLayoutId() {
    return R.layout.fragment_flow;
  }

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    edit_search_user = (EditText) rootView.findViewById(R.id.edit_search_user);
    edit_search_user.setOnKeyListener(keyListener);
    view_empty = (TextView) rootView.findViewById(R.id.view_empty);
    recycler_user = (RecyclerView) rootView.findViewById(R.id.recycler_user);
    recycler_user.addItemDecoration(
        new DividerDecoration(getContext(), DividerDecoration.VERTICAL_LIST));
    adapter = new UserSearchAdapter(new ArrayList<UserCard>());
    adapter.setListener(this);
    recycler_user.setLayoutManager(new LinearLayoutManager(getContext()));
    recycler_user.setAdapter(adapter);
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new FollowPresenter(this));
  }

  @Override public void setPresenter(FollowContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  @Override public String getSearchName() {
    return edit_search_user.getText().toString().trim();
  }

  @Override public void showList(List<UserCard> userCards) {
    adapter.clearList();
    adapter.addUserList(userCards);
  }

  @Override public void notify(int count, UserCard userCard) {
    adapter.setUserCard(count, userCard);
  }

  @Override public void setEmptyShow(boolean isShow) {
    recycler_user.setVisibility(isShow ? View.GONE : View.VISIBLE);
    view_empty.setVisibility(isShow ? View.VISIBLE : View.GONE);
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }

  @Override public void showText(int strId) {
    ToastUtil.show(strId);
  }

  /**
   * 软键盘确定监听，隐藏软键盘调用搜索
   */
  private View.OnKeyListener keyListener = new View.OnKeyListener() {
    @Override public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
        InputMethodManager inputMethodManager =
            (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager.isActive()) {
          inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
        presenter.search();
      }
      return false;
    }
  };

  @Override public void addUser(UserCard userCard) {
    this.userCard = userCard;
    CategoryActivity.startForResult(FollowFragment.this, CATEGORY_RESULT);
  }
}
